﻿#ifndef CUSTOMRECTITEM_H
#define CUSTOMRECTITEM_H


#include <QGraphicsObject>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QCursor>

class CustomRectItem : public QGraphicsObject
{
    Q_OBJECT

public:
    explicit CustomRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr);

    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;

    // 新增：设置编辑模式
    void setEditable(bool editable);
    bool isEditable() const;

    // 枚举保持不变
    enum Handle {
        TopLeft, Top, TopRight, Left, Right, BottomLeft, Bottom, BottomRight, None
    };

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
    QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;


private:
    QRectF m_rect;
    bool m_isSelected;
    bool m_isEditable; // 新增：编辑模式状态
    Handle m_currentHandle;
    QPointF m_dragStart;

    void setHandleCursor(Handle handle);
    QRectF handleRect(Handle handle) const;
};

#endif // CUSTOMRECTITEM_H
